package com.xzz.string;

/**
 * @author: hhz
 * @create: 2022-02-23 11:01
 * 917. 仅仅反转字母
 **/
public class ReverseOnlyLetters {
    public static void main(String[] args) {
        System.out.println(reverseOnlyLetters("a-bC-dEf-ghIj"));
        System.out.println(reverseOnlyLetters("ab-cd"));
        System.out.println(reverseOnlyLetters("Test1ng-Leet=code-Q!"));

        System.out.println(reverseOnlyLetters("7_28]"));


        System.out.println((true || false) && false);
        System.out.println(true || false && false);
    }
    public static String reverseOnlyLetters(String s) {
        int left = 0;
        int right = s.length() - 1;
        char[] chars = s.toCharArray();
        while (left < right) {
            while (left < right && !Character.isLetter(chars[left])) left++;
            while (left < right && !Character.isLetter(chars[right])) right--;
            if (left < right) {
                char c = chars[left];
                chars[left++] = chars[right];
                chars[right--] = c;
            }

        }
        return String.valueOf(chars);

    }
    public static String reverseOnlyLetters2(String s) {
        int left = 0;
        int right = s.length() - 1;
        char[] chars = s.toCharArray();
        while (left < right) {
            char l = chars[left];
            char r = chars[right];
            boolean lf = false;
            boolean rf = false;
            if ((l > 64 && l < 91) || (l > 96 && l < 123)) {
                lf = true;
            }
            if ((r > 64 && r < 91) || (r > 96 && r < 123)){
                rf = true;
            }
            if(lf && rf){
                char temp = l;
                chars[left] = r;
                chars[right] = temp;
                left++;
                right--;
            }else if(!lf && rf){
                left++;
            }else if(lf && !rf){
                right--;
            }else if(!lf&&!rf){
                left++;
                right--;
            }
        }
        return String.valueOf(chars);

    }
}
